/*
 * conexionRmiJdbc.java
 *
 * Created on 31 de mayo de 2007, 8:48
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package utiles;
import java.sql.*;
/**
 *
 * @author admin
 */
//Esta debe ir en el paquete del Servidor
public class conexionRmiJdbc {
    public java.sql.Connection conexion;
    public java.sql.Statement sentencia;
   
    /** Creates a new instance of conexionRmiJdbc */
    public conexionRmiJdbc( String ip, String instancia, String base, String usuario, String password, boolean opcion ){
        try{
            
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();    
            String conexx="jdbc:sqlserver://"+ip+";"+
                    "instanceName="+instancia+";"+"databaseName="+base+";"+
                    "user="+usuario+";password="+password+";"+"port=1290;";
            System.out.println(conexx);
            conexion=java.sql.DriverManager.getConnection(conexx);
            System.out.println(conexx);
            conexion.setAutoCommit(false);
            
        }catch( Exception er ){
            System.out.println( "Error al cargar driver" );
            er.printStackTrace();
        }
        
        if( opcion ){
            
            try{
                sentencia=conexion.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE,
                        java.sql.ResultSet.CONCUR_UPDATABLE);
            }catch( Exception er ){
                System.out.println( "Error al tipo de driver Sensitive" );
                er.printStackTrace();                
            }           
            
        }else{
            
            try{                    
                sentencia=conexion.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
                        java.sql.ResultSet.CONCUR_READ_ONLY);                       
            }catch( Exception er ){
                System.out.println( "Error al tipo de driver Insensitive" );
                er.printStackTrace();                
            }
            
        }   
    }
    public conexionRmiJdbc(){
        
    }
        
    public java.sql.ResultSet consultar( String query ){        
        java.sql.ResultSet resultset;        
        try{
            resultset=sentencia.executeQuery(query);
        }catch( java.sql.SQLException er ){            
            er.printStackTrace();
            resultset = null;
        }
        return resultset;        
    }
    
    public static java.sql.ResultSet query( String q ){
        java.sql.ResultSet r = null;
        try{
            Class.forName("org.objectweb.rmijdbc.Driver").newInstance(); 
            System.out.println("pasa");
            Connection conn = DriverManager.getConnection( "jdbc:rmi://" + "127.0.0.1" + ":" + "1236" + "/jdbc:odbc:" + "pruebita", "sa", "680141" );
            System.out.println("pasa");
            Statement sta = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
                            java.sql.ResultSet.CONCUR_READ_ONLY); 
            r = sta.executeQuery( q );
        }catch( Exception er ){
            System.out.println("Error"  );
            er.printStackTrace();
        }
        return r;
    }    
    
    public void cerrar(){
        
        try{
            conexion.close();
        }catch( java.sql.SQLException er ){           
            er.printStackTrace();
        }
        
    }   
    
}
